package cn.agiledata.bank.myspace.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.PeerInnerAccount;
import cn.agiledata.bank.myspace.exception.MySpaceException;
import cn.agiledata.bank.myspace.form.AccountUserInfoForm;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBMgrFactory;

public class TrxObjectAction extends AbstractMySpaceAction {
    public ActionForward trxObjectList(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String accountId = getAccountId(request);
        List trxObjectList;
        if (accountId == null) {
            trxObjectList = new ArrayList();
        } else {
            trxObjectList = getMySpaceService().getTrxObjectList(accountId);
        }
        request.setAttribute("trxObjectList", trxObjectList);
        return mapping.findForward("trxObjectList");
    }

    public ActionForward deleteTrxObject(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IPBException, Exception {
        String acc_id = request.getParameter("acc_id");
        if (acc_id != null)
            getMySpaceService().delPeerAccount(acc_id);
        return mapping.findForward("trxObjectList");
    }

    public ActionForward addTrxObject(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        AccountUserInfoForm theForm = (AccountUserInfoForm) form;
        long id = WeatherUtil.getAccountIdFromSession(request);
        theForm.setId(new Long(id).toString());
        return mapping.findForward("addTrxObject");
    }

    public ActionForward submitTrxObject(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IPBException, Exception {
        AccountUserInfoForm theForm = (AccountUserInfoForm) form;
        long id = WeatherUtil.getAccountIdFromSession(request);
        Account acct = WeatherUtil.getAccountByEmail(theForm.getEmail());
        AccountTemplateInter at = (AccountTemplateInter) IPBMgrFactory
                .getMgr("accountTemplate");
        if (at.ifHasAccountInPeer(acct.getId().toString(), String.valueOf(id))) {
            throw MySpaceException.EXISTED_PEER_ACCOUNT;
        }
        PeerInnerAccount pah = at.addAccountInPeer(acct, Long.toString(id), "1");
        at.saveAccountInPeer(pah);
        return mapping.findForward("trxObjectList");
    }
}
